package com.grsu.phonestation.dao.impl;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.grsu.phonestation.dao.UserDAO;
import com.grsu.phonestation.dao.impl.base.AbstractHibernateDAO;
import com.grsu.phonestation.domain.User;

@Repository
public class UserDAOImpl<U extends User> extends AbstractHibernateDAO<U, Integer>
		implements UserDAO<U> {

	@SuppressWarnings("unchecked")
	protected Class<U> getGenericEntityClass() {
		return (Class<U>) User.class;
	}

	@SuppressWarnings("unchecked")
	@Override
	public U findById(Integer userId) {
		Criteria criteria = getSession().createCriteria(
				this.getGenericEntityClass(), "user").add(
				Restrictions.eq("id", userId));
		return (U) criteria.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public U findByCredentials(String login, String password) {
		Criteria criteria = getSession()
				.createCriteria(this.getGenericEntityClass())
				.add(Restrictions.eq("login", login))
				.add(Restrictions.eq("password", password));
		return (U) criteria.uniqueResult();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public U getUserByLogin(String login) {
		
		Criteria criteria = getSession().createCriteria(this.getGenericEntityClass());
		criteria.add(Restrictions.eq("login", login));

		return (U) criteria.uniqueResult();

	}
	
	@SuppressWarnings("unchecked")
	@Override
	public U getUserByEmail(String email) {
		
		Criteria criteria = getSession().createCriteria(getGenericEntityClass());
		criteria.add(Restrictions.eq("email", email));

		return (U) criteria.uniqueResult();

	}

}